package com.airn.siege.lion.custom.widget

import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.airn.siege.lion.lib.widget.scroll.picker.CommonPopWindow
import com.airn.siege.lion.lib.widget.scroll.picker.DictInfo
import com.airn.siege.lion.lib.widget.scroll.picker.PickerScrollView
import com.airn.siege.lion.lib.widget.scroll.picker.SecondPickerScrollView

/**
 * Author: Airn
 * Create On: 2024-04-24
 * E-mail: 1275218537@qq.com
 */
object BottomPickerViewUtils {

    fun setAddressSelectorPopup(activity: AppCompatActivity, v: View) {
        val list: MutableList<String> = ArrayList()
        for (i in 0..19) {
            val itemData = "item: $i"
            list.add(itemData)
        }
        val screenHeight = activity.resources.displayMetrics.heightPixels
        CommonPopWindow.newBuilder()
            .setView(R.layout.pop_picker_selector_bottom)
            //.setAnimationStyle(R.style.AnimUp)
            .setBackgroundDrawable(BitmapDrawable())
            .setSize(ViewGroup.LayoutParams.MATCH_PARENT, Math.round(screenHeight * 0.3f))
            .setViewOnClickListener(CommonPopWindow.ViewClickListener { mPopupWindow, view, _ ->
                val imageBtn: TextView = view.findViewById(R.id.img_guanbi)
                val addressSelector: PickerScrollView = view.findViewById(R.id.address)

                // 设置数据，默认选择第一条
                addressSelector.setData(list)
                addressSelector.setSelected(0)
                //滚动监听
                addressSelector.setOnSelectListener { pickers ->
                    Toast.makeText(activity, "" + pickers, Toast.LENGTH_SHORT).show()
                }
                //完成按钮
                imageBtn.setOnClickListener {
                    mPopupWindow.dismiss()
                }
            })
            .setBackgroundDarkEnable(true)
            .setBackgroundAlpha(0.7f)
            .setBackgroundDrawable(ColorDrawable(999999))
            .build(activity)
            .showAsBottom(v)
    }

    fun secondOrderLinkagePopup(activity: AppCompatActivity, v: View, callback: (DictInfo?, DictInfo?) -> Unit) {
        val level = arrayListOf<DictInfo>()
        level.add(
            DictInfo(
                id = "1",
                dictTypeId = "",
                code = "1",
                name = "存在建设行为但可不处置的情形",
                filter = "0",
            )
        )
        level.add(
            DictInfo(
                id = "2",
                dictTypeId = "",
                code = "2",
                name = "持续监测的情形",
                filter = "0",
            )
        )
        level.add(
            DictInfo(
                id = "3",
                dictTypeId = "",
                code = "3",
                name = "其他可不处置的情形",
                filter = "0",
            )
        )
        level.add(
            DictInfo(
                id = "4",
                dictTypeId = "",
                code = "4",
                name = "不改变原用地性质的光伏用地",
                filter = "1",
            )
        )
        level.add(
            DictInfo(
                id = "5",
                dictTypeId = "",
                code = "5",
                name = "用于农业生产的简易棚",
                filter = "1",
            )
        )
        level.add(
            DictInfo(
                id = "6",
                dictTypeId = "",
                code = "6",
                name = "村庄范围内",
                filter = "1",
            )
        )
        level.add(
            DictInfo(
                id = "7",
                dictTypeId = "",
                code = "7",
                name = "零星堆土堆料的图斑或地块",
                filter = "2",
            )
        )
        level.add(
            DictInfo(
                id = "8",
                dictTypeId = "",
                code = "8",
                name = "地下管网（廊）建设形成的图斑或地块",
                filter = "2",
            )
        )
        level.add(
            DictInfo(
                id = "9",
                dictTypeId = "",
                code = "9",
                name = "紧急用地图斑或地块",
                filter = "2",
            )
        )
        level.add(
            DictInfo(
                id = "10",
                dictTypeId = "",
                code = "10",
                name = "灾毁地图斑或地块",
                filter = "3",
            )
        )
        level.add(
            DictInfo(
                id = "11",
                dictTypeId = "",
                code = "11",
                name = "因卫片监测技术问题错误提取的图斑或地块",
                filter = "3",
            )
        )
        level.add(
            DictInfo(
                id = "12",
                dictTypeId = "",
                code = "12",
                name = "因农业结构调整产生的耕地内部用途调整的图斑或地块",
                filter = "3",
            )
        )

        val level0 = arrayListOf<DictInfo>().apply {
            for (item in level) {
                if (item.filter == "0") {
                    add(item)
                }
            }
        }

        val level1 = arrayListOf<DictInfo>().apply {
            for (item in level) {
                if (item.filter == "1") {
                    add(item)
                }
            }
        }

        val level2 = arrayListOf<DictInfo>().apply {
            for (item in level) {
                if (item.filter == "2") {
                    add(item)
                }
            }
        }

        val level3 = arrayListOf<DictInfo>().apply {
            for (item in level) {
                if (item.filter == "3") {
                    add(item)
                }
            }
        }
        var level0Selected: DictInfo? = null
        var level1Selected: DictInfo? = null
        val screenHeight = activity.resources.displayMetrics.heightPixels
        CommonPopWindow.newBuilder()
            .setView(R.layout.pop_picker_second_selector_bottom)
            //.setAnimationStyle(R.style.AnimUp)
            .setBackgroundDrawable(BitmapDrawable())
            .setSize(ViewGroup.LayoutParams.MATCH_PARENT, Math.round(screenHeight * 0.3f))
            .setViewOnClickListener(CommonPopWindow.ViewClickListener { mPopupWindow, view, _ ->
                val imageCancelBtn: TextView = view.findViewById(R.id.img_cancel)
                val imageOkBtn: TextView = view.findViewById(R.id.img_ok)

                val addressSelector1: SecondPickerScrollView = view.findViewById(R.id.p1)
                val addressSelector2: SecondPickerScrollView = view.findViewById(R.id.p2)

                // 设置数据，默认选择第一条
                addressSelector1.setData(level0)
                addressSelector1.setSelected(0)

                addressSelector2.setData(level1)
                addressSelector2.setSelected(0)

                //滚动监听
                addressSelector1.setOnSelectListener { pickers ->
                    // Toast.makeText(activity,""+pickers, Toast.LENGTH_SHORT).show()
                    if (level0.indexOf(pickers) == 0) {
                        addressSelector2.setData(level1)
                    } else if (level0.indexOf(pickers) == 1) {
                        addressSelector2.setData(level2)
                    } else if (level0.indexOf(pickers) == 2) {
                        addressSelector2.setData(level3)
                    } else {
                        addressSelector2.setData(level1)
                    }
                    // 设置数据，默认选择第一条
                    addressSelector2.setSelected(0)
                    level0Selected = pickers

                }

                addressSelector1.setOnSelectListener { pickers ->
                    level1Selected = pickers
                }
                //取消按钮
                imageCancelBtn.setOnClickListener {
                    mPopupWindow.dismiss()
                }
                //完成按钮
                imageOkBtn.setOnClickListener {
                    mPopupWindow.dismiss()
                    callback(level0Selected, level1Selected)
                }
            })
            .setBackgroundDarkEnable(true)
            .setBackgroundAlpha(0.7f)
            .setBackgroundDrawable(ColorDrawable(999999))
            .build(activity)
            .showAsBottom(v)
    }
}